{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# LangChain + Klavis AI Integration\n",
        "\n",
        "This tutorial demonstrates how to build AI agents using LangChain with Klavis Strata MCP servers for enhanced functionality.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Prerequisites\n",
        "\n",
        "Before we begin, you'll need:\n",
        "\n",
        "- **OpenAI API key** - Get at [openai.com](https://openai.com/)\n",
        "- **Klavis API key** - Get at [klavis.ai](https://klavis.ai/)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Install the required packages\n",
        "%pip install -q klavis python-dotenv langchain-mcp-adapters langgraph langchain-openai\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\n",
        "import webbrowser\n",
        "from klavis import Klavis\n",
        "from klavis.types import McpServerName\n",
        "from langchain_openai import ChatOpenAI\n",
        "from langchain_mcp_adapters.client import MultiServerMCPClient\n",
        "from langgraph.prebuilt import create_react_agent\n",
        "\n",
        "# Set environment variables\n",
        "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY\"  # Replace with your actual OpenAI API key\n",
        "os.environ[\"KLAVIS_API_KEY\"] = \"YOUR_KLAVIS_API_KEY\"  # Replace with your actual Klavis API key\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 1: Create Strata MCP Server\n",
        "\n",
        "Create a unified MCP server that combines multiple services (Gmail and YouTube) for enhanced agent capabilities.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "klavis_client = Klavis(api_key=os.getenv(\"KLAVIS_API_KEY\"))\n",
        "\n",
        "# Create a Strata MCP server with Gmail and YouTube integrations\n",
        "response = klavis_client.mcp_server.create_strata_server(\n",
        "    user_id=\"demo_user\",\n",
        "    servers=[McpServerName.GMAIL, McpServerName.YOUTUBE],\n",
        ")\n",
        "\n",
        "print(f\"🔗 Strata MCP server created at: {response.strata_server_url}\")\n",
        "\n",
        "# Handle OAuth authorization if needed\n",
        "if response.oauth_urls:\n",
        "    for server_name, oauth_url in response.oauth_urls.items():\n",
        "        webbrowser.open(oauth_url)\n",
        "        print(f\"🔐 Opening OAuth authorization for {server_name}: {oauth_url}\")\n",
        "        input(f\"Press Enter after completing {server_name} OAuth authorization...\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 2: Create LangChain Agent with MCP Tools\n",
        "\n",
        "Set up the LangChain agent with tools from the Strata MCP server.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Create MCP client\n",
        "mcp_client = MultiServerMCPClient({\n",
        "    \"strata\": {\n",
        "        \"transport\": \"streamable_http\",\n",
        "        \"url\": response.strata_server_url,\n",
        "    }\n",
        "})\n",
        "\n",
        "# Get all available tools from Strata\n",
        "tools = await mcp_client.get_tools()\n",
        "\n",
        "# Setup LLM\n",
        "llm = ChatOpenAI(model=\"gpt-4o-mini\", api_key=os.getenv(\"OPENAI_API_KEY\"))\n",
        "\n",
        "# Create LangChain agent with MCP tools\n",
        "agent = create_react_agent(\n",
        "    model=llm,\n",
        "    tools=tools,\n",
        "    prompt=\"You are a helpful assistant that can use MCP tools.\",\n",
        ")\n",
        "\n",
        "print(\"🤖 LangChain agent created with MCP tools!\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 3: Run the Agent\n",
        "\n",
        "Use the agent to summarize a YouTube video and send the summary via email.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Configure your preferences\n",
        "my_email = \"your-email@example.com\"  # Replace with your email\n",
        "youtube_video_url = \"https://youtu.be/yebNIHKAC4A?si=1Rz_ZsiVRz0YfOR7\"  # Replace with your favorite video\n",
        "\n",
        "# Invoke the agent\n",
        "result = await agent.ainvoke({\n",
        "    \"messages\": [{\n",
        "        \"role\": \"user\", \n",
        "        \"content\": f\"summarize this video - {youtube_video_url} and send the summary to my email {my_email}\"\n",
        "    }],\n",
        "})\n",
        "\n",
        "# Print the final AI response\n",
        "print(\"\\n✅ Result:\")\n",
        "print(result[\"messages\"][-1].content)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Summary\n",
        "\n",
        "🎉 Congratulations! You've successfully created a LangChain agent that can:\n",
        "\n",
        "1. **Summarize YouTube videos** using the YouTube MCP server\n",
        "2. **Send emails** using the Gmail MCP server  \n",
        "3. **Coordinate multiple services** through Klavis Strata MCP integration\n",
        "\n",
        "This demonstrates the power of combining LangChain's agent framework with Klavis MCP servers for building sophisticated AI workflows that can interact with multiple external services seamlessly.\n"
      ]
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
